/**
 * Created by asdzheng on 2016/2/22.
 */
var atLeastLeftHeight = 750;

$(document).ready(function() {
    var page = $(".page");
    var count = page.data("count");
    var current = page.data("current");
    var ajax = page.data("ajax") || false;
    pageChange(page, count, current, ajax, requestBusinessList);

    if ($(".index-business-list").length < 6) {
        $(".index-content-left").css("height", atLeastLeftHeight + "px");
    }

    whiteBtnMouseEvent($(".business-order"));
});

$(".business-img").click(itemClick);
$(".index-business-name").click(itemClick);
$(".business-order").click(itemClick);

function itemClick() {

    var business_id = $(this).parents(".map1").attr("data-business_id");
    var category_id = $(this).parents(".map1").attr("data-category_id");
    jumpToDetial(business_id, category_id);
}

function jumpToDetial(business_id, category_id) {
    /* window.open(url) 
       打开一个新窗口
     */
    window.open("/detail/" + business_id + "-" + category_id + ".html");
}

function processResp(res, notScrollTotop) {
    var notScrollTotop = notScrollTotop || false;

    if (res.status == "0000") {

        var count = res.data.pages;
        var current = res.data.page;
        var page = $(".page");
        page.data("current", current);
        page.data("count", count);
        page.data("page_num", res.data.page_num);
        pageChange(page, count, current, "true", requestBusinessList);
        page.addClass("page-hide");

        var list = res.data.data;
        var divs = [];

        if (res.data.page_num < 6) {
            $(".index-content-left").css("height", atLeastLeftHeight + "px");
        } else {
            $(".index-content-left").css("height", "auto");
        }

        if (list.length > 0) {
            for (var i = 0; i < list.length; i++) {
                var item = list[i];

                var image_url = "../images/no_pic.png.jpg"/*tpa=http://www.quyundong.com/gosport/index/images/no_pic.png*/;
                if (item.image_url != '') {
                    image_url = item.image_url;
                }

                var icon_url_pc = "../images/no_pic.png.jpg"/*tpa=http://www.quyundong.com/gosport/index/images/no_pic.png*/;
                if (item.icon_url_pc != '') {
                    icon_url_pc = item.icon_url_pc;
                }

                var comment_avg = item.comment_avg * 1;
                var loveDivs = '';
                for (var j = 0; j < 5; j++) {
                    var imgDiv = '';
                    if (j < comment_avg) {
                        imgDiv = "<img src='../images/love-full.png'/*tpa=http://www.quyundong.com/gosport/index/images/love-full.png*/ class='love'>";
                    } else {
                        imgDiv = "<img src='../images/love.png'/*tpa=http://www.quyundong.com/gosport/index/images/love.png*/ class='love'>";
                    }

                    loveDivs = loveDivs + imgDiv;
                }

                var item_class = "";
                if (i > 9) {
                    item_class = 'map1 index-business-list-hide';
                } else {
                    item_class = 'map1 index-business-list';
                }

                var div = document.createElement("div");
                var promote_price;
                if (item.promote_price > 0)
                    promote_price = item.promote_price;
                else
                    promote_price = item.price;

                if (promote_price % 1 == 0)
                    promote_price = parseInt(promote_price);
                div.className = item_class;
                div.setAttribute("data-lng", item.longitude);
                div.setAttribute("data-lat", item.latitude);
                div.setAttribute("data-business_id", item.business_id);
                div.setAttribute("data-category_id", item.category_id);

                div.innerHTML = "<img src='" + image_url + "' class='business-img'><div class='business-text'>" +
                    "<div class='index-business-name map2' data-lng='" + item.longitude + "' data-lat='" + item.latitude + "'" +
                    "' data-category_id='" + item.category_id + "' data-business_id='" + item.business_id +
                    "' data-business_name='" + item.name + "'>" +
                    "<span class='business-num-text'>" + (i + 1) + " </span>" +
                    "<span class='business-title'>" + item.name + "</span>" +
                    "<img src='" + icon_url_pc + "' class='business-cat-img'></div>" +
                    "<div class='index-business-price'><span>趣运动价:</span> <span class='price'>" + promote_price + "元" + "</span></div>" +
                    "<div class='index-business-address'><span>地址:</span> <span class='address'>" + item.address + "</span></div></div>" +
                    "<div class='business-comment'><div class='comment'>" + loveDivs +
                    "<span class='comment-num'>(" + item.comment_count + "评论)</span><span class='comment-score'>" + (item.comment_avg*1).toFixed(1) + "</span></div>" +
                    "<div class='business-order'><div class='order' " + ">立即预订</div></div></div>";
                divs.push(div);
            }
            for (var k = divs.length - 1; k >= 0; k--) {
                $(".index-content-left").prepend(divs[k]);

            }
            //TODO
            $(".business-img").click(itemClick);
            $(".index-business-name").click(itemClick);
            $(".business-order").click(itemClick);
            if (!isIELow()) {
                if (!!$(".map2")[0]) {
                    myOpts1 = {
                        dom: $(".index-maps")[0],
                        list: $(".map2"),
                        data: {
                            centerX: $(".map2").eq(0).attr("data-lng"),
                            centerY: $(".map2").eq(0).attr("data-lat"),
                            setMapZooom: 13
                        }
                    }
                } else {
                    myOpts1 = {
                        dom: $(".index-maps")[0],
                        list: [],
                        data: {
                            centerX: 113.23,
                            centerY: 23.16,
                            setMapZooom: 13
                        }
                    }
                }
                var myMap1 = new UseAmap(myOpts1);
            }


            whiteBtnMouseEvent($(".business-order"));
        } else {
            var empty_page = "<div class='empty-content'><span class='empty-text'>找不到你想要的场馆，请你换个条件试试~</span></div>";
            $(".index-content-left").prepend(empty_page);
        }

        if (!notScrollTotop) {
            var toTop;
            if ($(".index-nav").length == 0)
                toTop = 0;
            else
                toTop = $(".index-nav").offset().top - 84;

            $(window).scrollTop(toTop);
        }
    }
}
